package com.beiben.experiment.tool.controller;

import java.util.List;

import com.beiben.experiment.equipment.domain.Equipment;
import com.beiben.experiment.equipment.domain.StateVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.beiben.experiment.tool.domain.Tool;
import com.beiben.experiment.tool.service.IToolService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 工具信息Controller
 * 
 * @author 熊国强
 * @date 2020-11-11
 */
@RestController
@RequestMapping("/experiment/tool")
public class ToolController extends BaseController
{
    @Autowired
    private IToolService toolService;

    /**
     * 查询工具信息列表
     */
    @PreAuthorize("@ss.hasPermi('experiment:tool:list')")
    @GetMapping("/list")
    public TableDataInfo list(Tool tool)
    {
        startPage();
        List<Tool> list = toolService.selectToolList(tool);
        return getDataTable(list);
    }

    /**
     * 导出工具信息列表
     */
    @PreAuthorize("@ss.hasPermi('experiment:tool:export')")
    @Log(title = "工具信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Tool tool)
    {
        List<Tool> list = toolService.selectToolList(tool);
        ExcelUtil<Tool> util = new ExcelUtil<Tool>(Tool.class);
        return util.exportExcel(list, "tool");
    }

    /**
     * 获取工具信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('experiment:tool:query')")
    @GetMapping(value = "/{toolId}")
    public AjaxResult getInfo(@PathVariable("toolId") Long toolId)
    {
        return AjaxResult.success(toolService.selectToolById(toolId));
    }

    /**
     * 新增工具信息
     */
    @PreAuthorize("@ss.hasPermi('experiment:tool:add')")
    @Log(title = "工具信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Tool tool)
    {
        return toAjax(toolService.insertTool(tool));
    }

    /**
     * 修改工具信息
     */
    @PreAuthorize("@ss.hasPermi('experiment:tool:edit')")
    @Log(title = "工具信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Tool tool)
    {
        return toAjax(toolService.updateTool(tool));
    }

    /**
     * 删除工具信息
     */
    @PreAuthorize("@ss.hasPermi('experiment:tool:remove')")
    @Log(title = "工具信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{toolIds}")
    public AjaxResult remove(@PathVariable Long[] toolIds)
    {
        return toAjax(toolService.deleteToolByIds(toolIds));
    }

    /**
     * 导入工具信息
     */
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        ExcelUtil<Tool> util = new ExcelUtil<Tool>(Tool.class);
        return util.importTemplateExcel("工具信息");
    }

    @Log(title = "工具信息导入", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('experiment:tool:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<Tool> util = new ExcelUtil<Tool>(Tool.class);
        List<Tool> toolList = util.importExcel(file.getInputStream());
        String message = toolService.importTool(toolList, updateSupport);
        return AjaxResult.success(message);
    }

    /**
     * 修改工具信息 使用状态
     */
//    @PreAuthorize("@ss.hasPermi('experiment:tool:state')")
    @PostMapping("/changeState")
    public AjaxResult changeState(@RequestBody StateVo stateVo)
    {
        return toAjax(toolService.updateToolState(stateVo.getIds(),stateVo.getState()));
    }
}
